package com.mlk.rbac.controller;

import com.mlk.core.constants.Constant;
import com.mlk.core.util.ServerResponse;
import com.mlk.core.util.ServletUtils;
import com.mlk.core.util.SessionUtils;
import com.mlk.rbac.Service.LoginService;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;

/**
 * 系统登录页
 * @author xulu
 */
@Controller
@Slf4j
public class LoginController {

    @Resource
    private LoginService loginService;

    /**
     * 系统登录
     * @param username
     * @param password
     * @return
     */
    @ResponseBody
    @PostMapping(value = "/rbac/login")
    public ServerResponse login(@RequestParam("username") String  username ,@RequestParam("password") String password){
        return this.loginService.login(username, password);
    }

    /**
     * 返回系统的首页
     * @return
     */
    @GetMapping(value = "/rbac/main")
    public String  main(){
        return "rbac/main";
    }

    /**
     * 用户注销
     * @return
     */
    @ResponseBody
    @GetMapping("/rbac/logout")
    public ServerResponse logout(){
        try {
            SecurityUtils.getSubject().logout();
        }catch (Exception e){
            log.error("注销出现异常",e);
            return  ServerResponse.fail("注销失败");
        }
        return ServerResponse.ok();
    }
}
